package top.tangyh.lamp.care.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.tangyh.basic.base.R;
import top.tangyh.basic.context.ContextUtil;
import top.tangyh.basic.exception.BizException;
import top.tangyh.lamp.care.mapper.BCareContentMapper;
import top.tangyh.lamp.care.service.BCareContentService;
import top.tangyh.basic.base.service.impl.SuperServiceImpl;
import top.tangyh.lamp.care.manager.BCareContentManager;
import top.tangyh.lamp.care.entity.BCareContent;
import top.tangyh.lamp.care.vo.query.QueryCareContentByPidAndTimeParam;
import top.tangyh.lamp.care.vo.save.BCareContentSaveVO;
import top.tangyh.lamp.care.vo.update.BCareContentUpdateVO;
import top.tangyh.lamp.care.vo.result.BCareContentResultVO;
import top.tangyh.lamp.care.vo.query.BCareContentPageQuery;
import top.tangyh.lamp.common.utils.ConvertUtil;
import top.tangyh.lamp.common.utils.SecurityUtil;
import top.tangyh.lamp.common.utils.YDateUtils;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 业务实现类
 * 护理记录内容表
 * </p>
 *
 * @author zqb
 * @date 2024-08-02 10:21:07
 * @create [2024-08-02 10:21:07] [zqb] [代码生成器生成]
 */
@Slf4j
@RequiredArgsConstructor
@Service
@Transactional(readOnly = true)
public class BCareContentServiceImpl extends SuperServiceImpl<BCareContentManager, Long, BCareContent> implements BCareContentService {

    private final BCareContentMapper bCareContentMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addOrUpdateCareContent(BCareContentSaveVO careContentPO) {
        if (null != careContentPO.getId()) {
            BCareContent old = bCareContentMapper.selectById(careContentPO.getId());


            if (null != SecurityUtil.checkRecordAuth(old.getCreatedBy(), old.getUpdatedBy(), ContextUtil.getUserId())) {
                throw new BizException(SecurityUtil.checkRecordAuth(old.getCreatedBy(), old.getUpdatedBy(), ContextUtil.getUserId()));
            }

        } else {
            if (bCareContentMapper.checkByTimeAndPatientIdentAndRecordBy(careContentPO.getPatientIdent(), careContentPO.getRecordTime(), careContentPO.getRecordBy())) {
                throw new BizException("此时间点已有护理记录");
            }
            Date time = YDateUtils.stringToDate(YDateUtils.format(new Date(YDateUtils.getTimestampOfDateTime(careContentPO.getRecordTime())), YDateUtils.DATE_TIME_MINUTE_PATTERN) + ":00", YDateUtils.DATE_TIME_PATTERN);
            careContentPO.setRecordTime(YDateUtils.getDateTimeOfTimestamp(time.getTime()));
        }

        BCareContent bCareContent = ConvertUtil.convert(careContentPO, BCareContent.class);
        return bCareContentMapper.insertOrUpdate(bCareContent);
    }


    @Override
    public List<BCareContentResultVO> queryCareContentByPidAndTime(QueryCareContentByPidAndTimeParam queryCareContentByPidAndTimeParam) {

        QueryWrapper<BCareContent> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(BCareContent::getPatientIdent, queryCareContentByPidAndTimeParam.getPatientId())
                .eq(BCareContent::getHospitalNumber, queryCareContentByPidAndTimeParam.getHospitalNumber());
        wrapper.lambda().eq(null != queryCareContentByPidAndTimeParam.getType(), BCareContent::getType, queryCareContentByPidAndTimeParam.getType());
        wrapper.apply("date_format (record_time,'%Y-%m-%d') = date_format('" + DateUtil.formatDateTime(queryCareContentByPidAndTimeParam.getTime()) + "','%Y-%m-%d')");
        wrapper.lambda().orderByDesc(BCareContent::getRecordTime);
        List<BCareContent> result = this.list(wrapper);
        List<BCareContentResultVO> resultVo=ConvertUtil.convertList(result,BCareContentResultVO.class);

        return resultVo;

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveList(List<BCareContent> hljl, Long recordTime, String patientIdent,String operator,String account,String userId) {
        bCareContentMapper.deleteOlds(recordTime,patientIdent,operator,account,userId);
        if(CollectionUtil.isEmpty(hljl)){
            return;
        }
        saveBatch(hljl);
    }


}


